package com.yinshan.printer.service;

import com.nexgo.oaf.apiv3.APIProxy;
import com.nexgo.oaf.apiv3.DeviceEngine;
import com.nexgo.oaf.apiv3.SdkResult;
import com.nexgo.oaf.apiv3.device.printer.AlignEnum;
import com.nexgo.oaf.apiv3.device.printer.OnPrintListener;
import com.nexgo.oaf.apiv3.device.printer.Printer;
import com.yinshan.printer.bean.PrintData;
import com.yinshan.printer.utils.Logs;

import java.util.ArrayList;

/**
 * 新国都POS机打印服务
 */
public class N5PrintService extends BaseIntentService {

    public DeviceEngine deviceEngine;
    private Printer printer;

    private final int FONT_SIZE_SMALL = 20;
    private final int FONT_SIZE_NORMAL = 24;
    private final int FONT_SIZE_BIG = 32;

    @Override
    void initPrint() {
        deviceEngine = APIProxy.getDeviceEngine();
        printer = deviceEngine.getPrinter();
        printer.initPrinter();
    }

    @Override
    void print(ArrayList<PrintData> printDatas) {
        if (null == printer) {
            Logs.d(TAG, "print is null!!!");
            return;
        }
        if (printer.getStatus() != SdkResult.Success) {
            if (printer.getStatus() == SdkResult.Printer_PaperLack) {
                showToast("打印机缺纸");
            }
            return;
        }

        for (PrintData item : printDatas) {
            Logs.i(TAG, "print:" + item.toString());
            printer.appendPrnStr(item.getText(), getFont(item.getTextSize()), getAlign(item.getTextAlign()), item.isBold());
        }
        //走纸 单位像素
        printer.feedPaper(40);
        Logs.d(TAG, "开始打印");
        printer.startPrint(true, new OnPrintListener() {
            @Override
            public void onPrintResult(int retCode) {
                switch (retCode) {
                    case SdkResult.Success:
                        break;
                    case SdkResult.Printer_PaperLack:
                        showToast("打印缺纸");
                        break;
                    case SdkResult.Printer_TooHot:
                        showToast("打印机过热");
                        break;
                    case SdkResult.Printer_UnFinished:
                        showToast("打印未完成");
                        break;
                    default:
                        Logs.i(TAG, "onPrintResult:" + retCode);
                        break;
                }
            }
        });
    }

    private int getFont(PrintData.Font myFont) {
        int font = FONT_SIZE_NORMAL;
        switch (myFont) {
            case BIG:
                font = FONT_SIZE_BIG;
                break;
            case SMALL:
                font = FONT_SIZE_SMALL;
                break;
            default:
                break;
        }
        return font;
    }

    private AlignEnum getAlign(PrintData.Align myAlign) {
        AlignEnum align = AlignEnum.CENTER;
        switch (myAlign) {
            case LEFT:
                align = AlignEnum.LEFT;
                break;
            case RIGHT:
                align = AlignEnum.RIGHT;
                break;
            default:
                break;
        }
        return align;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != printer) {
            printer = null;
        }
        if (null != deviceEngine) {
            deviceEngine = null;
        }
    }
}
